package com.jiuyuejiu.func_pic;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jiuyuejiu.R;
import com.jiuyuejiu.base.BaseActivity;
import com.jiuyuejiu.application.ConstValues;
import com.jiuyuejiu.func_pic.domain.PicStc;

import java.lang.ref.SoftReference;
import java.util.List;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;


// 展示网络的多张照片
public class LocalPicViewPagerActivity extends BaseActivity implements View.OnClickListener {


    private ViewPager image;
    private TextView tv_text;
    private TextView allnum;

    private List<PicStc> images;
    private int selectposition;


    private static final RequestOptions BANNER_OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)// 缓存SOURC和RESULT
            .dontAnimate()// 移除所有的动画
            // .fitCenter()// 该api可能 铺不满整个ImageView控件
            .centerCrop()// 按比例放大/缩小,铺满整个ImageView控件
            .placeholder(R.drawable.shape_solid_cccccc)// 占位图
            //.transform(new CenterCrop(), new RoundedCorners(18))
            ;

    private String username;
    private String password;

    MyHandler handler;

    /**
     * 接收子线程消息的 Handler
     */
    public class MyHandler extends Handler {

        // 软引用
        SoftReference<LocalPicViewPagerActivity> fragmentRef;

        public MyHandler(LocalPicViewPagerActivity fragment) {
            fragmentRef = new SoftReference<LocalPicViewPagerActivity>(fragment);
        }

        @Override
        public void handleMessage(Message msg) {
            LocalPicViewPagerActivity fragment = fragmentRef.get();
            if (fragment == null) {
                return;
            }

            Bundle bundle = msg.getData();
            super.handleMessage(msg);
            // 处理UI 变化
            switch (msg.what) {
                // 提示信息
                case ConstValues.WAIT0:
                    // 关闭缓冲界面
                    // finishAty();
                    break;
                case ConstValues.WAIT1:
                    // 关闭缓冲界面
                    // LatteLoader.stopLoading();
                    break;

                default:
                    break;
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_pic_viewpager);

        // 接收传递过来的参数
        initCreate();
        // 初始化视图
        initView();
        // 初始化数据
        initData();


    }

    private void initCreate() {

        /*// 上一页面设置数据
        Intent intent = null;
        intent = new Intent(this, RePwdActivity.class);
        Bundle bundl = new Bundle();
        intent.putExtra("username", "");
        intent.putExtra("password", "");
        startActivity(intent);*/

        // 本页面获取数据
        Intent i = getIntent();
        images = (List<PicStc>) i.getSerializableExtra("images");
        selectposition = (int) i.getSerializableExtra("position");
    }

    private void initView() {
        image = (ViewPager) findViewById(R.id.pic_viewpager_vp_pic);
        tv_text = (TextView) findViewById(R.id.pic_viewpager_tv_text);
        allnum = (TextView) findViewById(R.id.pic_viewpager_tv_allnum);

    }

    private void initData() {
        handler = new MyHandler(this);
        image.setAdapter(new Myadapter3());
        image.setCurrentItem(selectposition);
        tv_text.setText(images.get(selectposition).getPicname());
        allnum.setText(images.get(images.size() - 1).getPicname());

        image.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv_text.setText(images.get(position).getPicname());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }


    class Myadapter3 extends PagerAdapter {

        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            final PicStc stc = images.get(position);
            View view = View.inflate(LocalPicViewPagerActivity.this, R.layout.item_pic_viewpager, null);

            ImageView imageview3 = (ImageView) view.findViewById(R.id.item_pic_viewpager_img_detail);
            Glide.with(LocalPicViewPagerActivity.this)
                    .load(stc.getPicurl())
                    .apply(BANNER_OPTIONS)
                    .into(imageview3);

            imageview3.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    // downPicDate(picurl);
                    return false;
                }
            });
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /*// 返回
            case R.id.picsnetpath_showpic_rl_back:
                this.finish();
                break;
            case R.id.yd_mine_repwd_btn_submit://
                break;*/
        }
    }

}
